package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *@Author QZY
 *@Date 2020/4/14 15:36
 *@Version v1.0
 *@Description 菜单业务控制器
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 查询菜单
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id") Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"查询菜单成功", menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }

    /**
     * 编辑菜单
     * @param menu
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,"编辑菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑菜单失败");
        }
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Result delete(@RequestParam("id") Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,"删除菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单失败");
        }
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,"添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加菜单失败");
        }
    }

    /**
     * 查询分页数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = menuService.pageQuery(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, null);
        }
    }

}
